package com.hertz.iris.view;

import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.OperationCanceledException;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

import com.github.kevinsawicki.wishlist.SingleTypeAdapter;
import com.hertz.iris.Injector;
import com.hertz.iris.R;
import com.hertz.iris.authenticator.LogoutService;
import com.hertz.iris.core.NormalAlarmService;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.ui.ItemListFragment;
import com.hertz.iris.ui.ThrowableLoader;
import com.hertz.iris.util.Ln;
import com.hertz.iris.view.alarm.NormalAlarmActivity;

public class NormalAlarmsFragment extends ItemListFragment<NormalAlarm>{

	@Inject protected NormalAlarmService normalAlarmService;
    @Inject protected LogoutService logoutService;

    @Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.inject(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText(R.string.no_alarms);

        emptyView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent newAlarmIntent = new Intent(getActivity(), NormalAlarmActivity.class);
                startActivity(newAlarmIntent);
				
			}
		});
    }

    @Override
    protected void configureList(Activity activity, ListView listView) {
        super.configureList(activity, listView);

        listView.setFastScrollEnabled(true);
        listView.setDividerHeight(0);
    }

    @Override
    public void onDestroyView() {
        setListAdapter(null);

        super.onDestroyView();
    }

    @Override
    public Loader<List<NormalAlarm>> onCreateLoader(int id, Bundle args) {
        final List<NormalAlarm> initialItems = items;
        return new ThrowableLoader<List<NormalAlarm>>(getActivity(), items) {

            @Override
            public List<NormalAlarm> loadData() throws Exception {
                try {
                    if(getActivity() != null) {
                        return normalAlarmService.loadAllNormalAlarm();
                    } else {
                        return Collections.emptyList();
                    }

                } catch (OperationCanceledException e) {
                	Ln.e(e);
                    Activity activity = getActivity();
                    if (activity != null)
                        activity.finish();
                    return initialItems;
                }
            }
        };
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
//    	NormalAlarm normalAlarm = ((NormalAlarm) l.getItemAtPosition(position));
//
//        startActivity(new Intent(getActivity(), NewsActivity.class).putExtra(NEWS_ITEM, normalAlarm));
    	
    }

    @Override
    protected int getErrorMessage(Exception exception) {
        return R.string.error_loading_normalalarm;
    }

	@Override
	protected SingleTypeAdapter<NormalAlarm> createAdapter(
			List<NormalAlarm> items) {
		return new NormalAlarmListAdapter(getActivity().getLayoutInflater(), items);
	}
}
